<?php
	require('config.php');
	$email = $_POST["email"];
	$stmt = $connection->prepare("select id from user_id_passwd where email=?");
	$stmt->bind_param("s", $email);
	$stmt->execute();
	$stmt->store_result();
	$row_cnt = $stmt->num_rows;
	if ($row_cnt == 0)
	{
        echo "NOUSER";
        return;
	}
	else
	{
        $stmt->bind_result($id);
        $stmt->fetch();
        $pass = createPass();
		$md5 = substr(guid(), 1, 36);
        $stmt = $connection->stmt_init();
	    $stmt = $connection->prepare("select id from user_forget_password where id=?");
	    $stmt->bind_param("s", $id);
	    $stmt->execute();
	    $stmt->store_result();
	    $row_cnt = $stmt->num_rows;
        if ($row_cnt == 0)
        {
            $stmt = $connection->stmt_init();
	        $stmt = $connection->prepare("insert into user_forget_password(id, passwd, uuid, valid) values(?, ?, ?, 0)");
	        $stmt->bind_param("sss", $id, $pass, $md5);
	        $stmt->execute();
        }
        else
        {
            $stmt = $connection->stmt_init();
	        $stmt = $connection->prepare("update user_forget_password set passwd=?, uuid=?, valid=0 where id=?");
	        $stmt->bind_param("sss", $pass, $md5, $id);
	        $stmt->execute();
        }
	    /*$stmt = $connection->prepare("update user_id_passwd set passwd=?, md5=? where email=?");
        $stmt->bind_param("sss", $pass, $md5, $email);
        $stmt->execute();*/
	    sendEmail($email, $pass, $md5);
        echo "true";
        return;
	}
	
	function guid()
	{
		if (function_exists('com_create_guid')){
			return com_create_guid();//window下
		}else{
			mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
			$charid = strtoupper(md5(uniqid(rand(), true)));
			$hyphen = chr(45);// "-"
			$uuid = chr(125)// "{"
					.substr($charid, 0, 8).$hyphen
					.substr($charid, 8, 4).$hyphen
					.substr($charid,12, 4).$hyphen
					.substr($charid,16, 4).$hyphen
					.substr($charid,20,12)
					.chr(123);// "}"
			return $uuid;
		}
	}
    function createPass()
    {
        $pass = rand(100000, 999999);
        return (string)$pass;
    }
	
	function sendEmail($e, $u, $md5)
	{
		require("class.phpmailer.php");
		
		$mail = new PHPMailer();
		
		$mail->IsSMTP(); // set mailer to use SMTP
		$mail->Mailer = "smtp";
		$mail->CharSet = "UTF-8";
		$mail->Host = "smtp.163.com";
		$mail->Port = 25;
		$mail->SMTPAuth = true; // turn on SMTP authentication
		$mail->Username = "videobook@163.com"; // SMTP username
		$mail->Password = "10126301"; // SMTP password 
		 
		$mail->From     = "videobook@163.com";
		$mail->AddAddress($e); 
		 
		$mail->Subject  = "忘记密码";
		$mail->IsHTML(true);
		$mail->Body     = "<p>点击下面的链接，并填写新密码：<br/><br/><a href='http://x.apexlab.org/videohunter/forget_password_activate.php?uuid=" . $md5 . "'>" . $md5 . "</a></p><p>该链接只能使用一次！</p>";
        $mail->Body = " <div style='position:relative;width:500px; height:400px; background-color:#DDD;padding-left:20px;padding-right:10px;'> <div style='position:relative;margin-left:auto;margin-right:auto;width:200px;height:100px;'><img src='http://shiliu.tv/ico/logo4.png'/></div> <div> <div style='font: normal 16px Helvetica,Arial,sans-serif;font-weight:bold;'>欢迎来到石榴tv！<br/><br/> 点击以下链接，设置新密码！<br/><br/><a href='http://shiliu.tv/forget_password_activate.php?uuid=" . $md5 . "'>http://shiliu.tv/forget_password_activate.php?uuid=" . $md5 ."</a><br/><br/></div> <div style='font: normal 12px Helvetica,Arial,sans-serif;font-weight:bold;color:#444;'>如果以上链接无法访问，请将该网址复制并粘贴至新的浏览器窗口中。<br/><br/> 该链接只能使用一次！<br/><br/>这只是一封系统自动发出的邮件，请不要直接回复。<br/><br/></div> </div> </div> ";
        $mail->FromName = "石榴网";
		$mail->WordWrap = 50;  
		 
		if(!$mail->Send()) {
			echo 'Message was not sent.';
			echo 'Mailer error: ' . $mail->ErrorInfo;
		} else {
			echo 'Message has been sent.';
        }
	}
?>
